package org.smile.homework02.arthas.oneCase;

import lombok.Data;

import java.util.ArrayList;

@Data
public class ParkingLot {

    //1.0生成停车场
    public ParkingLot(Integer parkingSpaceNumber) {
        this.parkingSpaces = new ArrayList<>();
        for (int i = 0; i < parkingSpaceNumber; i++) {
            ParkingSpace parkingSpace = new ParkingSpace();
            parkingSpace.describe();
            this.parkingSpaces.add(parkingSpace);
        }
    }

    //1.1生成停车位（若干 -> 数组）
    private ArrayList<ParkingSpace> parkingSpaces;

    private Integer total = 0;

    //记录进入停车场的车辆
    //记录时间
    private ArrayList<VehicleMark> vehicleMarks = new ArrayList<>();  //null

    //1.2收费标准(方法)
    public Integer countingPrice(Vehicle vehicle) {
        String type = vehicle.getType();
        switch (type) {
            case "大":
                return 15;
            case "中":
                return 10;
            case "小":
            default:
                return 5;
        }
    }

    //===================
    //***形成交互
    //*承载交互的变量
    //*维护变量更新的方法

    //记录进入停车场的车辆
    //记录时间
    public void handleEnterParkingLot(Vehicle vehicle, Integer time) {
        VehicleMark vehicleMark = new VehicleMark();
        vehicleMark.setVehicle(vehicle);
        vehicleMark.setTime(time);
        this.vehicleMarks.add(vehicleMark);
    }

    public void handleLeaveParkingLot(Vehicle vehicle) {
        ArrayList<Integer> remarks = new ArrayList<>();
        for (int i = 0; i < this.vehicleMarks.size(); i++) {
            VehicleMark vehicleMark = this.vehicleMarks.get(i);
            if (vehicleMark.getVehicle().getNumber().equals(vehicle.getNumber())) {
                remarks.add(i);
            }
        }

        for (int i = 0; i < remarks.size(); i++) {
            this.vehicleMarks.remove(remarks.get(i));
        }
    }

    public void handleLeaveParkingLot(Vehicle vehicle, Integer time) {
        total += countAmount(vehicle, time);

        ArrayList<Integer> remarks = new ArrayList<>();
        for (int i = 0; i < this.vehicleMarks.size(); i++) {
            VehicleMark vehicleMark = this.vehicleMarks.get(i);
            if (vehicleMark.getVehicle().getNumber().equals(vehicle.getNumber())) {
                remarks.add(i);
            }
        }

        for (int i = 0; i < remarks.size(); i++) {
            this.vehicleMarks.remove(remarks.get(i));
        }
    }

    //车辆进入停车位的时候，与停车位交互
    public void handleEnterParkingSpace(Vehicle vehicle, Integer parkingSpaceNumber) {
        ParkingSpace parkingSpace = this.parkingSpaces.get(parkingSpaceNumber - 1);
        parkingSpace.handleInParkingSpace(vehicle);
    }

    public void handleLeaveParkingSpace(Integer parkingSpaceNumber) {
        ParkingSpace parkingSpace = this.parkingSpaces.get(parkingSpaceNumber - 1);
        parkingSpace.handleOutParkingSpace();
    }


    //1.3计算停车费用（方法）
    public Integer countAmount(Vehicle vehicle, Integer time) {
//        VehicleMark vehicleMark = new VehicleMark();
//        vehicleMark.setVehicle(vehicle);
//        int i = this.vehicleMarks.indexOf(vehicleMark);

        VehicleMark vehicleMark = new VehicleMark();

        for (VehicleMark vehicleMark1 : this.vehicleMarks) {
            if (vehicleMark1.getVehicle().getNumber().equals(vehicle.getNumber())) {
                vehicleMark = vehicleMark1;
            }
        }

        Integer recordTime = vehicleMark.getTime();

        return countingPrice(vehicle) * (time - recordTime);
    }

    public void runBusiness(Integer time) {
        for (ParkingSpace parkingSpace : this.getParkingSpaces()) {
            if (parkingSpace.isOccupied()) {
                if (parkingSpace.getVehicle().decideLeave()) {
                    Vehicle vehicle = parkingSpace.getVehicle();
                    vehicle.leaveParkingSpace(this, parkingSpace.getNumber());
                    vehicle.leaveParkingLot(this, time);
                }
            }
        }
    }

    //显示停车场状态（停车位、车辆）
    public void describe() {
        System.out.println("====================停车场状态========================");
        for (ParkingSpace parkingSpace : this.getParkingSpaces()) {
            parkingSpace.show();
        }
        System.out.println("当前总收入： " + this.getTotal());
        System.out.println("====================*********========================");
    }
}
